package missile;

import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import battlestein.Agent;
import battlestein.AgentManager;
/**
 * 
 * @author LocoRocoBattleStein
 *
 */
abstract public class Missile {
	private double posX;
	private double posY;
	private int finalX;
	private int finalY;
	private double dirX;
	private double dirY;
	private int diameter;
	
	private double velocity;
	
	private int effectVelocity;
	private int effectLife;
	
	private Image imgMissile;
	/**
	 * Constructor
	 * @param posX
	 * @param posY
	 * @param finalX
	 * @param finalY
	 * @param velocity
	 * @param effectLife
	 * @param effectVelocity
	 */
	public Missile(int posX, int posY, int finalX, int finalY, double velocity, int effectLife, int effectVelocity) {
		this.posX = posX;
		this.posY = posY;
		this.finalX = finalX;
		this.finalY = finalY;
		this.velocity = velocity;
		this.effectLife = effectLife;
		this.effectVelocity = effectVelocity;
		this.diameter = 5;
		
		setDirection();
	}

	public boolean hitTestMissile(AgentManager agentManager) {
		for(Agent a : agentManager.getAgentManager()) {
			if (hitTestMissile(a)) return true;
		}
		return false;
	}

	public boolean hitTestMissile(Agent a) {
		if ((Math.abs(a.getX() - posX) <= (a.getDiameter()/2)) &&
		    (Math.abs(a.getY() - posY) <= (a.getDiameter()/2))) {
			a.receiveMissile(this.effectLife, this.effectVelocity);
			return true;
		}
		else if ((Math.abs((double)finalX - posX) <= 1) &&
			     (Math.abs((double)finalY - posY) <= 1)) {
			return true;
		}
		return false;
	}

	public double getPosX() {
		return posX;
	}

	public void setPosX(double posX) {
		this.posX = posX;
	}

	public double getPosY() {
		return posY;
	}

	public void setPosY(double posY) {
		this.posY = posY;
	}

	public int getFinalX() {
		return finalX;
	}

	public void setFinalX(int finalX) {
		this.finalX = finalX;
	}

	public int getFinalY() {
		return finalY;
	}

	public void setFinalY(int finalY) {
		this.finalY = finalY;
	}

	public double getDirX() {
		return dirX;
	}

	public void setDirX(double dirX) {
		this.dirX = dirX;
	}

	public double getDirY() {
		return dirY;
	}

	public void setDirY(double dirY) {
		this.dirY = dirY;
	}

	public double getVelocity() {
		return velocity;
	}

	public void setVelocity(double velocity) {
		this.velocity = velocity;
	}

	public int getEffectVelocity() {
		return effectVelocity;
	}

	public void setEffectVelocity(int effectVelocity) {
		this.effectVelocity = effectVelocity;
	}

	public int getEffectLife() {
		return effectLife;
	}

	public void setEffectLife(int effectLife) {
		this.effectLife = effectLife;
	}
	
	public Image getImgMissile() {
		return imgMissile;
	}

	public void setImgMissile(String url) {
		try {
			this.imgMissile = ImageIO.read(new File(url));
		} catch (IOException e) {
			e.printStackTrace();
		};
	}
	
	public void setDirection() {
		double norm = Math.sqrt( Math.pow(finalX - posX, 2) + Math.pow(finalY - posY, 2) );
		dirX = (finalX - posX)/norm;
		dirY = (finalY - posY)/norm;
	}

	public void updatePosition() {
		posX += dirX * velocity;
		posY += dirY * velocity;
	}
	
	public void draw(Graphics g) {
		g.drawImage(imgMissile, (int)(posX - diameter/2.), (int)(posY - diameter/2.), diameter, diameter, null);
	}
}
